﻿using System;
using System.Globalization;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AT.IBMS.Framework.Controls.Designe
{
    internal static class DesignUtil
    {

        private const string contentHtml = "\r\n<tr>\r\n<td {1}>{0}</td>\r\n</tr>\r\n";
        private const string errorContainerHtml = "<span style=\"color:red\">{0}</span>";
        private const string standardDesignTimeHtml = "<table style=\"border:1px solid #CCCCCC;\" cellspacing=\"0\" cellpadding=\"0\">\r\n<tr>\r\n<td nowrap style=\"font:messagebox;background-color:#ffffff;color:#444444;{0}background-position:bottom;background-repeat:repeat-x;padding:4px;\"><strong>{1}</strong>{2}</td>\r\n</tr>{3}\r\n</table>";
        private const string subtitleContainerHtml = " - {0}";

        internal static string GetStandardDesignTimeHtml(IServiceProvider serviceProvider, string title, string subtitle, bool isError)
        {
            return GetContainerDesignTimeHtml(serviceProvider, title, subtitle, null, null, isError);
        }

        internal static string GetContainerDesignTimeHtml
            (
            IServiceProvider serviceProvider,
            string title, string subtitle,
            string content,
            string contentAttributes,
            bool isError
            )
        {
            string str;
            string str2;
            string str3;
            string str4;
            IResourceUrlGenerator service;

            str = string.Empty;

            if (serviceProvider != null)
            {
                service = (IResourceUrlGenerator)serviceProvider.
                    GetService(typeof(IResourceUrlGenerator));

                if (service != null)
                {
                    str = string.Format(CultureInfo.InvariantCulture, "background-image:url({0});", new object[] { service.GetResourceUrl(typeof(FrameworkManager), "AT.IBMS.Framework.Resources.Background.gif") });
                }
            }

            str2 = isError ? string.Format(CultureInfo.InvariantCulture, errorContainerHtml, new object[] { title }) : title;
            str3 = !string.IsNullOrEmpty(subtitle) ? string.Format(CultureInfo.InvariantCulture, subtitleContainerHtml, new object[] { subtitle }) : string.Empty;
            str4 = string.Empty;

            if (!string.IsNullOrEmpty(content) || !string.IsNullOrEmpty(contentAttributes))
            {
                str4 = string.Format(CultureInfo.InvariantCulture, contentHtml, new object[] { content, contentAttributes });
            }

            return string.Format(CultureInfo.InvariantCulture, standardDesignTimeHtml, new object[] { str, str2, str3, str4 });
        }

        internal static string GetWebControlStyleHtml(WebControl control)
        {

            StringBuilder sb;

            sb = new StringBuilder();

            if (control.BackColor != null)
            {
                sb.Append("background-color:");
                sb.Append(control.BackColor.Name);
            }

            if (sb.Length > 0)
            {
                sb.Insert(0, "style=\"");
                sb.Append("\"");
            }

            return sb.ToString();

        }

    }
}
